package com.jch.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jch.reggie.common.CustomExcepttion;
import com.jch.reggie.dto.SetmealDto;
import com.jch.reggie.entity.Category;
import com.jch.reggie.entity.Dish;
import com.jch.reggie.entity.Setmeal;
import com.jch.reggie.entity.SetmealDish;
import com.jch.reggie.mapper.SetmealMapper;
import com.jch.reggie.service.CategoryService;
import com.jch.reggie.service.SetmealDishService;
import com.jch.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
@Slf4j
public class SetmealServicelmpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;
    /**
     *  新增套餐，同时需要保存套餐和菜品的对应关系
     * @param setmealDto
     */
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息，
        this.save(setmealDto);
        //保存套餐和菜品的关联信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes=  setmealDishes.stream().map((item)->{
            item.setDishId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
    }

    /**
     * 新增套餐，同时需要保存套餐和菜品的对应关系
     * @param ids
     */
    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        //判断套餐状态是否可以删除
        LambdaQueryWrapper<Setmeal> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);

        int count=this.count(queryWrapper);
        if(count>0){
            //不能删除，抛出业务异常
            throw  new CustomExcepttion("套餐正在售卖中，不能删除");
        }
        //如果可以删除，先删除套餐数据
        this.removeByIds(ids);
        //删除关系表中数据
        LambdaQueryWrapper<SetmealDish> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(queryWrapper1);
    }

    /**
     * 根据id查询套餐信息和菜品信息
     * @param id
     * @return
     */

    @Override
    public SetmealDto getByIdWith(Long id) {
        //根据id查询套餐信息
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto=new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        //根据id查询套餐名称
        Setmeal byId = this.getById(id);
        Long categoryId = byId.getCategoryId();
        Category category = categoryService.getById(categoryId);
        String name = category.getName();
        //根据id查询菜品信息
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);

        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }

    /**
     * 更新菜品
     */
    @Transactional
    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        //更新套餐信息
        this.updateById(setmealDto);
        //清理套餐对应的菜品L
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);
        //更新菜品口味信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes= setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);

    }


}
